﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace HM
{
    [HotUpdate]
    [Description("退货单（销售合同）审核回写退货数量到调拨单")]
    public class BackToTHqty: AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            string FID = "", F_RXWU_Text="", FMATERIALID="", FQTY="";
            foreach(DynamicObject item in e.DataEntitys)
            {
                FID = Convert.ToString(item["ID"]);
                string SQL = "/*dialect*/" + string.Format(@"select t.F_RXWU_Text,t1.FMATERIALID,t1.FQTY from T_CRM_CONTRACT t
                left join T_CRM_CONTRACTENTRY t1 on t.FID=t1.FID
                where t.FID='{0}'", FID);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                if(DynamData.Count>0)
                {
                    for(int i=0;i<DynamData.Count;i++)
                    {
                        F_RXWU_Text = Convert.ToString(DynamData[i]["F_RXWU_Text"]);
                        FMATERIALID = Convert.ToString(DynamData[i]["FMATERIALID"]);
                        FQTY = Convert.ToString(DynamData[i]["FQTY"]);
                        string SQL0 = "/*dialect*/" + string.Format(@"update t set t.F_RXWU_Qty1='{0}' from T_STK_STKTRANSFERINENTRY t
                        left join T_STK_STKTRANSFERIN t1 on t.fid=t1.fid
                        where t1.F_RXWU_TEXT2='{1}' and t.FMATERIALID='{2}'", FQTY, F_RXWU_Text, FMATERIALID);
                        DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                    }
                }
            }
        }
    }
}
